package com.anysoft.api;

import com.anysoft.YouthLifeApplication;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheEntity;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;
import com.lzy.okgo.model.HttpParams;

import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import okhttp3.OkHttpClient;

/**
 * Created by lty on 2018/1/26.
 * okgo网络配置
 */

public class OkGoService {

    /**
     * 初始化网络通信服务
     */
    public static void init(YouthLifeApplication application) {

        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        //log相关
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo");
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);        //log打印级别，决定了log显示的详细程度
        loggingInterceptor.setColorLevel(Level.INFO);                               //log颜色级别，决定了log在控制台显示的颜色
        builder.addInterceptor(loggingInterceptor);                                 //添加OkGo默认debug日志

        //超时时间设置，默认60秒
        builder.readTimeout(30, TimeUnit.SECONDS);      //全局的读取超时时间
        builder.writeTimeout(30, TimeUnit.SECONDS);     //全局的写入超时时间
        builder.connectTimeout(30, TimeUnit.SECONDS);   //全局的连接超时时间

        //公共参数
        HttpParams params = new HttpParams();

        // 其他统一的配置
        OkGo.getInstance().init(application)                           //必须调用初始化
                .setOkHttpClient(builder.build())               //建议设置OkHttpClient，不设置会使用默认的
                .setCacheMode(CacheMode.NO_CACHE)               //全局统一缓存模式，默认不使用缓存，可以不传
                .setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE)  //全局统一缓存时间，默认永不过期，可以不传
                .setRetryCount(3)                     //全局统一超时重连次数，默认为三次，那么最差的情况会请求4次(一次原始请求，三次重连请求),不需要可以设置为0
//                .addCommonHeaders(headers)                      //全局公共头
                .addCommonParams(params);                        //全局公共参数
    }
}
